package com.apexedu.sync.node.impl;

import com.apexedu.biz.entity.TAccessAppReg;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.entity.TSysMenu;
import com.apexedu.sync.engine.ISyncContext;
import com.apexedu.sync.exception.AuCSyncException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by ly on 2015/1/13.
 */
public class BaseNode {
    protected Log log = LogFactory.getLog(this.getClass());//日志记录

    /**
     * 记录日志时获取操作人
     *
     * @param context
     * @return
     */
    protected String getUserInfo(ISyncContext context) {
        return "".equals(context.getUser().getUserloginid()) ? context.getAccessAppReg().getAppname() + "系统自动同步" : context.getAccessAppReg().getAppname();
    }


    /**
     * 获取业务系统根目录id，没有先添加
     *
     * @param context 接入系统
     * @return 根目录id
     * @throws Exception
     */
    protected String getParentMenuId(ISyncContext context) throws AuCSyncException {
        TAccessAppReg tAccessAppReg = context.getAccessAppReg();
        try {
            Object upmenuidO = DBUtil.queryFieldValue("select menuid from t_sys_menu t where t.belongtoapp=? and length(menuid)=6 and bizcode=menuid",
                    tAccessAppReg.getAppid());
            if (upmenuidO == null) { //该业务系统的根节点菜单不存在时创建根节点菜单
                upmenuidO = SeqFactory.getNewTreeIdByParentId("t_sys_menu", "menuid", "upmenuid", "000");
                TSysMenu menu = new TSysMenu();
                menu.setMenuid(upmenuidO.toString());
                menu.setUpmenuid("000");
                menu.setMenuname(tAccessAppReg.getAppname());
                menu.setMenupath(tAccessAppReg.getAppurl());
                menu.setBelongtoapp(tAccessAppReg.getAppid());
                menu.setOrdernum(10000 - Integer.parseInt(menu.getMenuid().substring(menu.getMenuid().length() - 2)) * 100);//默认排序值
                menu.setState("1");
                menu.setSrctype("1"); //菜单
                context.getBaseDao().save(menu);
            }
            return upmenuidO.toString();
        } catch (Exception e) {
        	e.printStackTrace();
            log.error("getParentMenuId error :" + e);
            throw new AuCSyncException(AuCSyncException.UNKNOWN_SERVICE_ERROR, e.getMessage());
        }
    }
}
